#!/usr/bin/perl

# $Id: 12_dbwrapper_hostschecker.t 205 2010-07-12 18:53:28Z dolbanavt $
# $Date: 2010-07-12 21:53:39 +0300 (Пнд, 12 Июл 2010) $
# $HeadURL: https://robosearch.googlecode.com/svn/branches/reborn/t/12_dbwrapper_hostschecker.t $
# $LastChangedRevision: 349 $

use FindBin qw( $RealBin );
use File::Spec::Functions qw( catfile );

BEGIN {
    push @INC, catfile( $RealBin, '..', 'base_lib' );
    push @INC, catfile( $RealBin, '..', 'lib' );
}

use JIP::ToolSet;
use English qw( -no_match_vars );
use Test::More 0.88;
use Const::Fast qw( const );
use Test::Exception;
use DBI;

const my $MODULE => 'DBWrapper::HostsChecker';

use_ok $MODULE;
require_ok $MODULE;

can_ok $MODULE, qw(
    new _init
    set_dbh get_dbh
    get_known_hosts
    add_new_host
    update_status_for
    update_netbios_name
);

{
    my $w = init_wrapper();

    ok $w, 'got object';

    isa_ok $w, $MODULE;
    isa_ok $w, 'DBWrapper';
    isa_ok $w, 'JIP::BaseClass';
    isa_ok $w, 'Class::Accessor::Fast';

    is ref $w->get_dbh, 'DBI::db', 'dbh accessor';

    throws_ok { $w->add_new_host() }
        qr{^ip_address\srequired}xms,
        'add_new_host( ip_address )';

    throws_ok { $w->update_status_for() }
        qr{^ip_address\srequired}xms,
        'update_status_for( ip_address )';

    throws_ok { $w->update_netbios_name(); }
        qr{^Required\soption\s'ip_address'\sis\snot\sprovided}xms,
        'update_netbios_name( ip_address )';

    throws_ok { $w->update_netbios_name( ip_address => q{} ); }
        qr{^Required\soption\s'netbios_name'\sis\snot\sprovided}xms,
        'update_netbios_name( netbios_name )';
}

#-- get_known_hosts ------------------------------------------------------------
{
    my $w = init_wrapper();

    $w->get_dbh->{'mock_add_resultset'} = [
        [ 'ip_address' ],
        [ '10.0.14.140' ],
        [ '192.168.1.1' ],
    ];

    my @hosts = $w->get_known_hosts();

    # resultsets testing
    is scalar @hosts, 2, 'correct number of resultsets';

    is $hosts[0], '10.0.14.140', 'returned ip_address in first resultset';
    is $hosts[1], '192.168.1.1', 'returned ip_address in second resultset';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       0,
       'correct number of parameters bound';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->get_known_hosts();
}
qr{cannot\sprepare:\sNo\sconnection\spresent}xms,
'"cannot_prepare" exception';

#-- add_new_host ---------------------------------------------------------------
{
    my $w = init_wrapper();

    my $result = $w->add_new_host('10.0.14.140');

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       1,
       'correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       '10.0.14.140',
       'correct value for parameter 1';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->add_new_host('10.0.14.140');
}
qr{cannot\sinsert\sdata:\sNo\sconnection\spresent}xms,
'"cannot_insert_data" exception';

#-- update_status_for ----------------------------------------------------------
{
    my $w = init_wrapper();

    my $result = $w->update_status_for('10.0.14.140');

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       1,
       'correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       '10.0.14.140',
       'correct value for parameter 1';
}

throws_ok {
    my $w = init_wrapper();

    $w->get_dbh->{'mock_can_connect'} = 0;

    $w->update_status_for ('10.0.14.140');
}
qr{cannot\sinsert\sdata:\sNo\sconnection\spresent}xms,
'"cannot_insert_data" exception';

#-- update_netbios_name --------------------------------------------------------
{
    my $w = init_wrapper();

    my $result = $w->update_netbios_name(
        ip_address   => '10.0.14.140',
        netbios_name => 'JIP',
    );

    is $result, 1, 'returned true';

    # history testing
    my $dbh_history = $w->get_dbh->{'mock_all_history'};

    is scalar @{ $dbh_history },
       1,
       'correct number of statements executed';

    is scalar @{ $dbh_history->[0]->bound_params },
       2,
       'correct number of parameters bound';

    is $dbh_history->[0]->bound_params->[0],
       'JIP',
       'correct value for parameter 1';

    is $dbh_history->[0]->bound_params->[1],
       '10.0.14.140',
       'correct value for parameter 2';
}

done_testing();

#-- subroutines ----------------------------------------------------------------
sub init_wrapper {
    my $dbh = DBI->connect( 'DBI:Mock:', q{}, q{} );

    $dbh->{'PrintError'} = 0;

    return $MODULE->new( dbh => $dbh );
}

#-- end ------------------------------------------------------------------------
